package Dao.Impl;

import Bean.FileBean;
import Dao.FileSqlOperation;
import Mapper.FileSqlOperationMapper;
import Utils.Dateoperation;
import Utils.FileOperation;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.util.List;
import java.util.UUID;

public class FileSqlOperationImpl implements FileSqlOperation {
    FileBean fileBean;
    FileOperation fileOperation;
    SqlSessionFactory sqlSessionFactory;
    SqlSession sqlSession;
    FileSqlOperationMapper fileSqlOperation;
    public FileSqlOperationImpl(FileOperation fileOperat){
        fileOperation =fileOperat;
    }
    public FileSqlOperationMapper getMybatisConfig(){
        //      加载Mybatis的配置
        String resource = "mybatis-config.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
         sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
         sqlSession = sqlSessionFactory.openSession(true);
         fileSqlOperation = sqlSession.getMapper(FileSqlOperationMapper.class);
        return fileSqlOperation;
    }

    @Override
    public void insertFileInformation() {
        FileSqlOperationMapper fileSqlOperationMapper=getMybatisConfig();
//        提取文件的相关数据信息
//        将数据存入bean中并且插入数据库
        fileBean =fileOperation.fileInfo();
        fileBean.setOrders(fileSqlOperationMapper.getOrder());
        fileSqlOperation.insert(fileBean);
        sqlSession.close();
    }

    @Override
    public List<FileBean> selectFile() {
        FileSqlOperationMapper fileSqlOperationMapper=getMybatisConfig();
        List<FileBean> list=fileSqlOperationMapper.selectAll();
        System.out.println(list);
        return list;
    }


    @Override
    public FileBean downloadOneBynameKey(String nameKey) {
        FileSqlOperationMapper fileSqlOperationMapper=getMybatisConfig();
        FileBean fileBean=fileSqlOperationMapper.downloadOne(nameKey);
        System.out.println("dao.sql操作"+fileBean);
        return fileBean;
    }

}
